﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using Core;
using ClassLibrary;

namespace TestProject
{
    public partial class NewReprodLink : Form
    {
        private Model model;
        private string phpath, natselpath, name;
        private bool b = false,trade=false;
        private decimal amount;
        private MyDeleg deleg;
        public NewReprodLink(Model m)
        {
            InitializeComponent();
            model = m;
        }
        public NewReprodLink(Model m, string p1, string p2, string n,decimal a)
        {
            InitializeComponent();
            model = m;
            phpath = p1;
            natselpath = p2;
            name = n;
            amount = a;
            b = true;
        }
        public NewReprodLink(Model m, string p1, string p2, string n, decimal a, MyDeleg del)
        {
            InitializeComponent();
            model = m;
            phpath = p1;
            natselpath = p2;
            name = n;
            amount = a;
            deleg = del;
            b = true;
            trade=true;
        }
        private void NewReprodLink_Load(object sender, EventArgs e)
        {
            if (b)
            {
                natsel_link_box.Text = name;
                natsel_link_box.Enabled = false;
            }
            else
            {
                foreach (NaturalSelectionLink link in model.model_natural_selection_links.Values)
                {
                    natsel_link_box.Items.Add(link.name);
                }
            }
        }

        private void create_butt_Click(object sender, EventArgs e)
        {
            if ((natsel_link_box.SelectedItem==null)&&(natsel_link_box.Text == ""))
            {
                MessageBox.Show("Please, choose natural selection link.");
                return;
            }
            if (System.IO.File.Exists("file_input\\" + model.model_name + "\\Links\\ReproductionLinks\\" + name_box.Text + ".xml"))
            {
                MessageBox.Show("A reproduction link with this name already exists. Please, choose another name");
                return;
            }
            if (!b)
            {
                foreach (NaturalSelectionLink link in model.model_natural_selection_links.Values)
                {
                    if ((string)natsel_link_box.SelectedItem == link.name)
                    {
                        natselpath = link.path;
                        break;
                    }
                }
            }
            XmlTextWriter writer = new XmlTextWriter("file_input\\" + model.model_name + "\\Links\\ReproductionLinks\\" + name_box.Text + ".xml", null);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartDocument();
            writer.WriteStartElement("link");
            writer.WriteAttributeString("Type", "characteristics_and_reproduction_events");
            writer.WriteAttributeString("Name", name_box.Text);
            writer.WriteAttributeString("PathToPhenotypeTable", phpath);
            writer.WriteAttributeString("PathToNaturalSelectionLink", natselpath);
            writer.WriteAttributeString("CharacteristicsAmount", amount.ToString());
            writer.WriteAttributeString("ReproductionEventsAmount", "1");
            writer.WriteStartElement("characteristic_names");
            if (!b)
            {
                PhenotypeTable phen_t = model.model_phenotype_tables[phpath];
                int[] values = new int[phen_t.characteristics_amount];
                int j = -1;
                foreach (CharacteristicDescription charac in phen_t.characteristic_table.Values)
                {
                    j++;
                    writer.WriteString(charac.name);
                    if (charac != phen_t.characteristic_table.Values.Last())
                        writer.WriteString(" ");
                    values[j] = charac.max_degree;
                }
                writer.WriteEndElement();
                writer.WriteStartElement("characteristic_value_diapasons");
                foreach (CharacteristicDescription charac in phen_t.characteristic_table.Values)
                {
                    writer.WriteString(charac.max_degree.ToString());
                    if (charac != phen_t.characteristic_table.Values.Last())
                        writer.WriteString(" ");
                }
                writer.WriteEndElement();
                writer.WriteStartElement("reproduction_event");
                writer.WriteAttributeString("Id", "0");
                writer.WriteAttributeString("Name", "Having 2 Children");
                writer.WriteAttributeString("ChildrenAmount", "2");
                writer.WriteStartElement("characteristic_sets");
                List<List<int>> comb =Functions.combine(values);
                foreach (List<int> l in comb)
                {
                    foreach (int i in l)
                    {
                        writer.WriteString(" " + i.ToString());
                    }
                    writer.WriteString(";");
                }
                writer.WriteEndElement();
                writer.WriteEndElement();
            }
            else
            {
                
                for (int i = 0; i < amount; i++)
                {
                    writer.WriteString("Char_" + i.ToString());
                    if (i != amount - 1)
                        writer.WriteString(" ");
                }
                writer.WriteEndElement();
                writer.WriteStartElement("characteristic_value_diapasons");
                for (int i = 0; i < amount; i++)
                {
                    writer.WriteString("0");
                    if (i != amount - 1)
                        writer.WriteString(" ");
                }
                writer.WriteEndElement();
                writer.WriteStartElement("reproduction_event");
                writer.WriteAttributeString("Id", "0");
                writer.WriteAttributeString("Name", "Having 2 Children");
                writer.WriteAttributeString("ChildrenAmount", "2");
                writer.WriteStartElement("characteristic_sets");
                for (int i = 0; i < amount; i++)
                {
                    writer.WriteString(" 0");
                }
                writer.WriteString(";");
                writer.WriteEndElement();
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
            MyXmlDocument doc = new MyXmlDocument();
            doc.LoadXml(System.IO.File.ReadAllText(model.model_path));
            XmlElement el;
            el = doc.CreateElement("link");
            el.SetAttribute("Id", model.model_natural_selection_links.Count.ToString());
            el.SetAttribute("Path", "file_input\\" + model.model_name + "\\Links\\ReproductionLinks\\" + name_box.Text + ".xml");
            doc.Increment(doc.GetElementsByTagName("links")[0].Attributes[3]);
            doc.Increment(doc.DocumentElement.Attributes[2]);
            doc.GetElementsByTagName("reproduction_links")[0].AppendChild(el);
            doc.Save(model.model_path);
            if (trade)
                deleg("file_input\\" + model.model_name + "\\Links\\ReproductionLinks\\" + name_box.Text + ".xml");
            Functions.add_node_func(new ReproductionLink("file_input\\" + model.model_name + "\\Links\\ReproductionLinks\\" + name_box.Text + ".xml"), name_box.Text, "reprodl");
            this.Close();            
        }
    }
}
